﻿using Microsoft.EntityFrameworkCore;
using MyWeb.TaskGrpc.Context;
using System.Linq.Expressions;
using MyWeb.TaskGrpc.Models;

namespace MyWeb.TaskGrpc.Context.Services
{
    public class JobsService : IJobsService
    {
        private readonly JobsContext db;
        //private readonly DbContext _db;

        public JobsService(JobsContext context)
        {
            db = context;
        }

        /// <summary>
        /// 添加Job
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddJob(Jobs model)
        {
            model.Id = 0;

            await db.AddAsync<Jobs>(model);
            var date = await db.SaveChangesAsync();
            if (date > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 根据条件查询Job列表
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public async Task<List<Jobs>> GetJobs(int page = 1, int pageSize = 20)
        {
            var data = from p in db.Set<Jobs>() select p;
            //var result = db.Set<Jobs>().FirstOrDefault();

            var items = data.Skip((page - 1) * pageSize).Take(pageSize);
            //总数
            long count = data.LongCount();
            //总页数
            long pageCount = (long)Math.Ceiling(count * 1.0 / pageSize);
            Console.WriteLine("========pageCount：", pageCount);
            //List<Jobs> result = (List<Jobs>)data;
            return await items.ToListAsync();

            //return await BaseDal.Queryable<Jobs>().Where(where).ToListAsync();
        }

        public Task<List<Jobs>> GetJobs(Expression<Func<Jobs, bool>> where)
        {
            //var data = db.Jobs.Where(where).ToListAsync();
            //.AsNoTracking() 指定不跟踪实体
            var data = db.Jobs.AsNoTracking().Where(where).ToListAsync();
            //var data = db.Jobs.AsNoTracking().FirstOrDefaultAsync(where);
            return data;
        }

        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Remove(Jobs model)
        {
            var job = db.Remove<Jobs>(model);
            var resu = await db.SaveChangesAsync();
            if (resu == 1)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 更新任务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> Update(Jobs model)
        {
            db.Update(model);
            var resu = await db.SaveChangesAsync();
            if (resu == 1)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 添加日志记录
        /// </summary>
        /// <param name="log"></param>
        /// <returns></returns>
        public async Task<bool> AddTaskLog(TaskLog log)
        {
            log.Id = 0;

            db.Add(log);
            var date = await db.SaveChangesAsync();
            if (date > 0)
            {
                return true;
            }
            return false;
        }
        public async Task<List<TaskLog>> GetTaskLog(long jobId, int page, int pageSize)
        {
            var data = from p in db.Set<TaskLog>()
                       where p.JobsId == jobId
                       orderby p.BeginDate descending
                       select p;
            //var result = db.Set<Jobs>().FirstOrDefault();

            var items = data.Skip((page - 1) * pageSize).Take(pageSize);
            //总数
            long count = data.LongCount();
            //总页数
            long pageCount = (long)Math.Ceiling(count * 1.0 / pageSize);
            Console.WriteLine("========pageCount：", pageCount);
            List<TaskLog> result = await items.ToListAsync();
            return result;
        }
    }
}
